﻿using System;
using System.Collections.Generic;
using System.Text;
using GeneratorLibrary.Client.Base;
using System.Data;

namespace MySqlGeneratorLibrary {
    public class MySqlFactory : IClientFactory {
        #region IClientFactory Members

        public string GetDefaultDACName(string server, string db) {
            return db + "MySqlDAC";
        }

        public BaseDAC NewClassGenDAC(string connectionString, string schema) {
            return new MySqlDAC(connectionString, schema);
        }

        public ClassBase NewDataClass(DataTable dt, string table, string classNamespace, string dacNamespace, string dacName) {
            return new MySqlDataClass(dt, table, classNamespace, dacNamespace, dacName);
        }
        public string getEmpeddedResourceOutputDAC_Path() {
            return "MySqlGeneratorLibrary.output_DAC.txt";
        }

        public string GetConnectionString(string server, string schema) {
            return "Server=" + server + ";Database=" + schema + ";Trusted_Connection=True;";
        }

        public string GetConnectionString(string server, string schema, string user, string password) {
            return "Server=" + server + ";Database=" + schema + ";User ID=" + user + ";Password=" + password + ";";
        }

        public string CatalogSchema {
            get { return "master"; }
        }

        public List<CrudTableBase> GetCrudTables(DataTable dt) {
            List<CrudTableBase> tables = new List<CrudTableBase>();
            foreach (DataRow dr in dt.Rows) {
                CrudTableBase table;
                if (tables.Count == 0 || tables[tables.Count - 1].TableName != dr["TableName"].ToString()) {
                    table = new MySqlCrudTable();
                    table.TableName = dr["TableName"].ToString();
                    tables.Add(table);
                } else {
                    table = tables[tables.Count - 1];
                }
                MySqlCrudColumn column = new MySqlCrudColumn(dr);
                table.Columns.Add((CrudColumnBase)column);
            }
            return tables;
        }
        public bool AllowsRunningScripts {
            get { return true; }
        }
        public bool ConnectionSupportsCreateProcs {
            get { return true; }
        }
        public string GetGOStatement() {
            return "GO";
        }
        #endregion
    }
}
